package org.example.book;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Controller
public class BookController {
    List<Book> BookList = new ArrayList<Book>();

    @RequestMapping("/")
    public String index() {
        return "index";
    }

    @RequestMapping("/add")
    public String add(Book book, Model model) {
        return "add";
    }

    @RequestMapping("/addBook")
    public String addBook(@ModelAttribute("book") Book book, BindingResult result, Model model) {
        log.info(book.getAuthor() + book.getPrice() + book.getBookName());
        boolean flag=false;
        // 检查是否为空
        if (book.getBookName() == null || book.getBookName().isEmpty()) {
            log.info("书名必须输入");
            model.addAttribute("bookNameValidateInfo", "书名必须输入");
            flag=true;
        }
        if (book.getAuthor() == null || book.getAuthor().isEmpty() || book.getAuthor().length() > 20) {
            log.info("作者字段的长度请保持在1-20");
            model.addAttribute("AuthorValidateInfo", "作者字段的长度请保持在1-20");
            flag=true;
        }
        try {
            if (Integer.parseInt(book.getPrice()) <= 0) {
                log.info("书籍价格应大于0");
                model.addAttribute("PriceValidateInfo", "书籍价格应大于0");
                flag=true;
            }
        } catch (NumberFormatException e) {
            log.info("价格必须为数字");
            model.addAttribute("PriceValidateInfo", "价格必须为数字");
            flag=true;
        }
        if(flag) return "add";
        BookList.add(book);
        // 后续的添加书籍逻辑...
        return "search";
    }


    @RequestMapping("/search")
    public String search() {
        return "search";
    }


// 导入其他需要的包

    @RequestMapping("/searchBook")
    public String searchBook(Book book, RedirectAttributes redirectAttributes,Model model) {
        log.info(book.getBookName());
        if (book.getBookName() == null || book.getBookName().isEmpty()) {
            log.info("书籍不存在");
            redirectAttributes.addFlashAttribute("bookNotExistInfo", "书籍不存在");
            return "redirect:/search";
        }
        for (Book b : BookList) {
            if (b.getBookName().equals(book.getBookName())) {
                log.info(b.getBookName() + "查询成功");
                model.addAttribute("book",b);
                return "searchBook"; // 如果找到书，返回书籍详情页面
            }
        }
        log.info("查询书籍不存在");
        redirectAttributes.addFlashAttribute("bookNotExistInfo", "书籍不存在");

        return "redirect:/search"; // 如果没有找到书，重定向到搜索页面
    }
    @RequestMapping("/list")
    public String list(Book book, RedirectAttributes redirectAttributes,Model model){
        model.addAttribute("BookList",BookList);
        return "list";
    }
    @RequestMapping("/deleteBook/{index}")
    public String deleteBook(@PathVariable int index, RedirectAttributes redirectAttributes) {
        if (index >= 0 && index < BookList.size()) {
            BookList.remove(index);
            redirectAttributes.addFlashAttribute("deleteMessage", "书籍删除成功");
        } else {
            redirectAttributes.addFlashAttribute("deleteMessage", "书籍删除失败");
        }
        redirectAttributes.addAttribute(BookList);
        return "redirect:/list";
    }


}
